<?php

declare(strict_types=1);

namespace App\Admin\Actions\Author;

use App\Models\Admin\Author;
use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class IdentAudit extends RowAction
{
    public $name = "作者审核";

    public function handle(Model $model, Request $request)
    {
        $adminId = $request->post("audit_id", "");
        $isForbidden = $request->post("is_forbidden", 2);
        $auditMsg = $request->post("audit_msg", "");
        $auditDate = date("Y-m-d H:i:s");
        $uid = $request->post("uid", "");

        $updateRow = Author::query()->where("uid", "=", $uid)->update([
            "audit_id" => $adminId,
            "audit_msg" => $auditMsg,
            "audit_at" => $auditDate,
            "is_forbidden" => $isForbidden,
            "audit_status" => $request->post("audit_status", 3),
        ]);
        if ($updateRow) {
            return $this->response()->success("审核成功")->refresh();
        }
        return $this->response()->error("审核失败");
    }

    public function form()
    {
        $uid = $this->getRow()->getAttribute("uid");
        $author = Author::query()->where("uid", "=", $uid)->first(["series_count", "created_at", "is_forbidden", "uid", "audit_status", "created_at"]);
        $this->hidden("uid", "数据id")->default($uid);
        $this->hidden("audit_id", "用户id")->default(Auth::guard('admin')->user()->toArray()["id"]);
        $this->text("created_at", "认证时间")->default($author->created_at)->readonly();
        $this->textarea("audit_msg", "审核信息")->default("审核通过")->required();
        $this->select('is_forbidden', '账号状态')->default($author->is_forbidden)->options([1 => "禁用", 2 => "启用"])->required();
        $this->select('audit_status', '审核状态')->default($author->audit_status)->options([1 => "审核通过", 2 => "审核驳回", 3 => "待审核中"])->required();
    }
}
